package com.me.extend.format

import java.text.DecimalFormat

object DecimalFormatUtil {

    const val DECIMAL_FORMAT_00 = "00"

    private val DECIMAL_THREAD_LOCAL by lazy {
        object : ThreadLocal<MutableMap<String, DecimalFormat>>() {
            override fun initialValue(): MutableMap<String, DecimalFormat> {
                return mutableMapOf()
            }
        }
    }

    fun getDecimalFormat(pattern: String): DecimalFormat {
        val sdfMap = DECIMAL_THREAD_LOCAL.get()!!
        var format = sdfMap[pattern]
        if (format == null) {
            format = DecimalFormat(pattern)
            sdfMap[pattern] = format
        }
        return format
    }
}

fun Int.decimalFormat(): String = DecimalFormatUtil.getDecimalFormat(DecimalFormatUtil.DECIMAL_FORMAT_00)
    .format(this)

fun Long.decimalFormat(): String = DecimalFormatUtil.getDecimalFormat(DecimalFormatUtil.DECIMAL_FORMAT_00)
    .format(this)
